package org.hongbo.springbootebsapi.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

@Service
public class DBConnection {

    @Value("$spring.datasource.primary.jdbc-url")
    private String jdbcUrl;

    @Value("$spring.datasource.primary.username")
    private String dbUser;

    @Value("$spring.datasource.primary.password")
    private String dbPassword;

    @Value("$spring.datasource.primary.driver-class-name")
    private String driverClassName;

    public  Connection getJdbcConnection() {
        Connection connection = null;
        try {

            Properties props = new Properties();

            props.put("user", dbUser);
            props.put("password", dbPassword);

            try {
                Class.forName(driverClassName).newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            connection = DriverManager.getConnection(jdbcUrl, props);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return connection;

    }

}
